<?php
class PagesController extends AppController {
	public $uses = array('Product','Category');
	public function beforeFilter() {
        parent::beforeFilter();		
		$this->Auth->allow();
    }
	
    public function home() {		
        $newest = $this->Product->find('all',array(
			'fields' => array('Product.*'),
			'conditions' => array(
				'Product.del_flag' => 0,				
			),			
			'order' => 'Product.created desc,product_code desc',			
			'limit' => 4,
		));
		$bestseller = $this->Product->find('all',array(
			'fields' => array('Product.*','sum(OrderDetail.amount) as tmp'),
			'conditions' => array(
				'Product.del_flag' => 0,				
			),
			'joins' => array(
				array(
					'table' => 'order_details',
					'alias' => 'OrderDetail',
					'conditions' => array('OrderDetail.product_id = Product.id'),
					'type' => 'inner',
				),
			),
			'order' => 'tmp desc',
			'group' => 'OrderDetail.product_id',			
			'limit' => 4,
		));
		$this->set(compact('newest','bestseller'));
    }
}
